#include "../include/admin_list.h"

static AdminNode *admin_head;
static AdminNode *tail;

void admin_list_init()
{
    admin_head = malloc(sizeof(AdminNode));
    if(admin_head == NULL)
    {
        printf("管理员链表头结点创建失败\n");
        exit(1);
    }
    admin_head->next = NULL;
    tail = admin_head;
}

void admin_list_insert_tail(const char *input)
{
    AdminNode *new_node = malloc(sizeof(AdminNode));
    if(new_node == NULL)
    {
        printf("管理员链表结点创建失败\n");
        exit(1);
    }

    memset(new_node->username, 0, sizeof(new_node->username));
    memset(new_node->password, 0, sizeof(new_node->password));

    char *p = strtok(input, "@");
    strncpy(new_node->username, p, sizeof(new_node->username));
    p = strtok(NULL, "@");
    strncpy(new_node->password, p, sizeof(new_node->password));

    new_node->next = tail->next;
    tail->next = new_node;
    tail = new_node;
}

bool find_admin(const char *username, const char *password)
{
    AdminNode *p = admin_head->next;
    while(p != NULL && strcmp(p->username, username) != 0 && strcmp(p->password, password) != 0)
    {
        p = p->next;
    }
    if(strcmp(p->username, username) == 0 && strcmp(p->password, password) == 0)
    {
        return true;
    }
    return false;
}